#include "title.hpp"

TitleScreen::TitleScreen()
{
}

TitleScreen::~TitleScreen()
{
}

static const int TITLELEN = 11;
static const char *TITLESTRS[TITLELEN] = 
{
	"c","a","s","t","l","e","v","a","r","i","a"
};

const wchar_t *SUBTITLE = L"T h e  A B O M I N A T I O N";

const wchar_t *PROMPT = L"PRESS ENTER";
 
#include "image.hpp"

static Image *titleimgs[TITLELEN];
static Image *fontimg;

#include "music.hpp"

bool TitleScreen::Open()
{
	scalex = 0;
	start = 0;

	int got = AllImgs.BatchGet(titleimgs, TITLELEN, "logo/", TITLESTRS);
	fontimg = AllImgs.Get("font");
	got += fontimg != 0;

	Mus.Close();
	Mus.Open("quiet");
	return true;
}

void TitleScreen::Close()
{
	AllImgs.Clear();
	Mus.Close();
}

#include "input.hpp"
#include "app.hpp"

void TitleScreen::In()
{
	if (Inp.CmdEdge(Inp.EXIT))
	{
		App.Exit();
	}
	else if (Inp.CmdTime(Inp.ENTER) == 1)
	{
		if (scalex < TITLELEN)
		{
			scalex = TITLELEN;
			Mus.Play(-1);
		}
		else if (!start)
		{
			++start;
			//Mus.Play(Mus.START);
		}
	}
}

#include "vec.hpp"

static const float TITLEPOSY = 8;
static const float TITLECENTX[TITLELEN] =
{
	3.5f, 7, 10.5f,	14, 17.5f, 21,
	24.5f, 28, 31.5f, 34, 36.5f
};

static const float DSCALEX = .125f;
static const int MUSTIME = 300;

#include "game.hpp"

#include "menu.hpp"

void TitleScreen::Update()
{
	if (scalex < TITLELEN)
	{
		scalex += DSCALEX;
		if (scalex >= TITLELEN)
			Mus.Play(-1);
	}
	if (start)
		++start;
	if (start >= MUSTIME)
	{
		App.ScreenChange(&Menu);
		return;
	}
}

#include "camera.hpp"
#include "video.hpp"
#include "audio.hpp"
#include <cmath>
#include <cwchar>

void TitleScreen::Out(float upfrac)
{
	if (start + 1 >= MUSTIME)
		return;

	Vid.Look(0, 0);
	Aud.Listen(Cam.AxHalf(X), Cam.AxHalf(Y));
	
	float sx = scalex;
	for (int i = 0; i < scalex; ++i)
	{
		if (!titleimgs[i])
			continue;

		float posx = TITLECENTX[i];
		float lsx = std::min(sx, 1.f);
		Vid.Scale(lsx, 1);
		posx -= .5f * lsx * titleimgs[i]->TileWidth();

		Vid.Pos(posx, TITLEPOSY);
		Vid.Pri(1);
		Vid.Flip(false, false);
		Vid.SpriteDraw(*titleimgs[i], 0);

		--sx;
	}

	Vid.Scale(1, 1);

	if (scalex >= TITLELEN)
	{
		Vid.Pos(6, 18);
		Vid.TilesDraw(*fontimg, SUBTITLE, std::wcslen(SUBTITLE), 1);
	}

	if (start % 16 < 8)
	{
		Vid.Pos(.5f * (Cam.size.x - std::wcslen(PROMPT)), 24);
		Vid.TilesDraw(*fontimg, PROMPT, std::wcslen(PROMPT), 1);
	}
}

TitleScreen Title;
